package com.tanhua.manage.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.manage.service.MessageAuditService;
import com.tanhua.manage.service.MessageServiceImpl;
import com.tanhua.manage.vo.CommentQueryParam;
import com.tanhua.manage.vo.LogQueryParam;
import com.tanhua.manage.vo.PageResult;
import com.tanhua.manage.vo.PersonInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 寒
 * @date 2021/6/6
 * @TIME:11:05
 */
@RestController
@RequestMapping("/manage/messages")
@Slf4j
public class MessageController {
    @Autowired
    MessageServiceImpl messageServiceImpl;

    @Autowired
    private MessageAuditService messageAuditService;

    @GetMapping
    public ResponseEntity<PageResult> queryMesageList(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                                      @RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pagesize,
                                                      @RequestParam("sortProp") String sortProp,
                                                      @RequestParam("sortOrder") String sortOrder,
                                                      @RequestParam(value = "id", required = false) Long id,
                                                      @RequestParam(value = "uid", required = false) Long uid,
                                                      @RequestParam(value = "sd", required = false) Long sd,
                                                      @RequestParam(value = "ed", required = false) Long ed,
                                                      @RequestParam(value = "state", required = false) String state) {
        PageResult pageResult = null;
        try {
            pageResult = messageAuditService.queryMessageList(pagesize, page, sortOrder, sortProp, id, sd, ed, state, uid);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.info("分页查询失败" + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @PostMapping("/pass")
    public ResponseEntity<String> messagePass(@RequestBody List<Long> idList) {
        try {
            String result = messageAuditService.messagePass(idList);
            if (StrUtil.isNotEmpty(result)) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.info("审核通过失败" + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @PostMapping("/reject")
    public ResponseEntity<String> messageReject(@RequestBody List<Long> idList) {
        try {
            String result = messageAuditService.messageReject(idList);
            if (StrUtil.isNotEmpty(result)) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.info("审核拒绝失败" + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @PostMapping("/revocation")
    public ResponseEntity<String> messageRevocation(@RequestBody List<Long> idList) {
        try {
            String result = messageAuditService.messageRevocation(idList);
            if (StrUtil.isNotEmpty(result)) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.info("撤销失败" + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @GetMapping("/{id}")
    public ResponseEntity<PersonInfoVo> queryPersonInfo(@PathVariable("id") Long id) {
        try {
            PersonInfoVo personInfoVo = messageAuditService.queryPersonInfo(id);
            if (ObjectUtil.isNotEmpty(personInfoVo)) {
                return ResponseEntity.ok(personInfoVo);
            }
        } catch (Exception e) {
            log.info("消息详情查询失败" + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @GetMapping("logs")
    public ResponseEntity<PageResult> queryLogs(LogQueryParam logQueryParam) {
        try {
            PageResult pageResult = messageServiceImpl.queryLogs(logQueryParam);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询日志列表出错~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    @GetMapping("comments")
    public ResponseEntity<PageResult> queryComments(CommentQueryParam commentQueryParam) {
        try {
            PageResult pageResult = messageServiceImpl.queryComment(commentQueryParam);
            System.out.println(pageResult);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询评论出错~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }

}
